package mathFrame

case class Sum(override val parameters: Value*) 
extends BinaryOperator('+', Arithmetic.sum, parameters: _*) {}

case class Sub(override val parameters: Value*) 
extends BinaryOperator('-', Arithmetic.sub, parameters: _*) {}

case class Prod(override val parameters: Value*) 
extends BinaryOperator('*', Arithmetic.prod, parameters: _*) {}

case class Div(override val parameters: Value*) 
extends BinaryOperator('/', Arithmetic.div, parameters: _*) {}

case class UM(override val parameters: Value*)
extends UnaryOperator('-', Arithmetic.unaryMinus, parameters: _*) {}

object Arithmetic {
  def sum(vals: Double*) = {
    vals length match {
      case 2 => vals(0) + vals(1)
      case _ => Double.NaN
    }
  }
  
  def sub(vals: Double*) = {
    vals length match {
      case 2 => vals(0) - vals(1)
      case _ => Double.NaN
    }
  }
  
  def prod(vals: Double*) = {
    vals length match {
      case 2 => vals(0) * vals(1)
      case _ => Double.NaN
    }
  }
  
  def div(vals: Double*) = {
    vals length match {
      case 2 => vals(0) / vals(1)
      case _ => Double.NaN
    }
  }
  
  def unaryMinus(vals: Double*) = {
    vals length match {
      case 1 => -vals(0)
      case _ => Double.NaN
    }
  }
  
  def createSum(parameters: Value*) = Sum(parameters: _*)
  
  def createSub(parameters: Value*) = Sub(parameters: _*)
  
  def createProd(parameters: Value*) = Prod(parameters: _*)
  
  def createDiv(parameters: Value*) = Div(parameters: _*)
  
  def createUM(parameters: Value*) = UM(parameters: _*)
  
  def init = {
    JimParsers addOperator('+', createSum)
    JimParsers addOperator('-', createSub)
    JimParsers addOperator('*', createProd)
    JimParsers addOperator('/', createDiv)
  }
}
